﻿        //revise修补动作系列
var ObjectsReviseBtn;
        function reviseRender() {
            var revises = $("#shopPanle [revise='true']");
            //加入常住内存

            ObjectsReviseBtn = (revises.after("<span class='reviseBtn'></span>")).next();
            var _reviseBtn = revises.next();

            _reviseBtn.addClass("reviseLabel");

            _reviseBtn.click(function (evt) {
                if ($(this).prev().attr("revise") == "true") {

                    reviseClick(evt);
                }
                
            })
        }

        function reviseClick(evt) {
            var it = evt.srcElement.previousSibling;
            reviseMaskRender("show", it);

            switch(it.id){
                case "shopSize":
                    $.get("/dict/getValueByName?name=shopSize", function (str) {
                        reviseMaskRender("hide");
                        //$(it).attr("revise", "start")
                        reviseForSelect($("#shopSize"), eval(str));
                    });
                    break;
                case "shopHead":
                    $.get("/dict/getValueByName?name=shopHead", function (str) {
                        reviseMaskRender("hide");
                        //$(it).attr("revise", "start")
                        reviseForSelect($("#shopHead"), eval(str));
                    });
                    break;
                case "shopClassMain" :

                    $.get("/dict/getValueByName?name=shopClass", function (str) {

                        reviseMaskRender("hide");
                        //$(it).attr("revise", "start");
                        reviseForRaido($("#shopClassMain"), eval(str));
                    });
                    break;
                case "shopClassMore":

                    $.get("/dict/getValueByName?name=shopClass", function (str) {

                        reviseMaskRender("hide");
                        //$(it).attr("revise", "start");
                        reviseForCheckbox($("#shopClassMore"), eval(str));
                    });
                    break;
                case "shopFit":

                    $.get("/dict/getValueByName?name=shopFit", function (str) {

                        reviseMaskRender("hide");
                        //$(it).attr("revise", "start");
                        reviseForCheckbox($("#shopFit"), eval(str));
                    });
                    break;
                case "shopTags":
                    $.get("/dict/getValueByName?name=shopTags_default",function(str){
                        reviseMaskRender("hide");
                        //$(it).attr("revise", "start");
                        reviseForTags($("#shopTags"),eval(str));

                    });
                    break;
                case "shopIntro":
                    $.get("/dict/getValueByName?name=shopTags_default", function (str) {
                        reviseMaskRender("hide");
                        //$(it).attr("revise", "start");
                        reviseForTextArea($(it), eval(str));

                    });
                    break;
                default:
                    reviseMaskRender("hide");
                    $(it).attr("revise", "start")
                    reviseForInput($(it));
            }


        };

        function reviseMaskRender(type, it) {
            it = $(it)
            if ($("#reviseMaskPanle").length==0) {
                $(document.body).append("<div id='reviseMaskPanle'></div>");

            }
            var reviseMaskDom = $("#reviseMaskPanle");
            if (type == "hide") { reviseMaskDom.css("display", "none"); return }

            elLocation(reviseMaskDom, it, 0); //定位
            reviseMaskDom.width(it.outerWidth());
            reviseMaskDom.height(it.outerHeight());
            reviseMaskDom.css("opacity", 0.3);
            reviseMaskDom.css("display", "block");


        }
        function reviseForTags($obj,json){
            var str = "<div id='"+$obj.attr("id") + "revise' class='shopTagsRevisePanle reviseForm'>";
            str +="<input name='shopTags' class='shopTagsRevise' value='"+$obj.html().replace(/<\/A><A>/g,",").replace("<A>","").replace("</A>","")+"' id='inputTags'/><div id='shopTagsTemp'>";
            for (var im in json){
                str += "<a class='shopTag' href='###' onclick=selectTagsAPP_act(this,'inputTags')>" + json[im].Key + "</a>";         
            }
            str += "</div>"
            str +="</div><div class='reviseSaveBtn' onclick=reviseSubmit('"+$obj.attr("id")+"',this.previousSibling.children(0).value,'tags')></div>"
            reviseFloat($obj, str);
            
        }
        function reviseForInput($obj) {
            var str = "<div id='" + $obj.attr("id") + "revise' class='reviseForm' >";
            str +="<input name='"+$obj.attr("id")+"' class='' value='" + $obj.attr("inputValue") + "'/>";
            str += "</div><div class='reviseSaveBtn' onclick=reviseSubmit('"+$obj.attr("id")+"',this.previousSibling.children(0).value)></div>";
            reviseFloat($obj,str);
        }
        function reviseForTextArea($obj){
            var str = "<div id='" + $obj.attr("id") + "revise' class='reviseForm' >";
            str +="<textarea name='"+$obj.attr("id")+"' class='' size='4' >"+$obj.attr("inputValue")+"</textarea>";
            str += "</div><div class='reviseSaveBtn' onclick=reviseSubmit('"+$obj.attr("id")+"',this.previousSibling.children(0).value,'textArea')></div>";
            reviseFloat($obj,str);        
        }

        function reviseForSelect($obj,json) {
            var str = "<div id='" + $obj.attr("id") + "revise' class='reviseForm' >";
            str += "<select name='"+$obj.attr("id")+"'>"
            var inputValue = $obj.attr("inputValue");
            for (var im in json) {

                str += "<option value='" + json[im].Key+"'"
                if (json[im].Key.toString() == inputValue) {
                    str += " selected='true'";
                }
                str += ">" + json[im].Value + "</option>";
            }
            str += "</select></div><div class='reviseSaveBtn' onclick=reviseSubmit('" + $obj.attr("id") + "',this.previousSibling.children(0).value,'select')></div>";

            reviseFloat($obj,str);

        }
        function reviseForCheckbox($obj, json) {

            var str = "<div id='" + $obj.attr("id") + "revise' class='reviseCheckBox reviseForm' style='min-width:200px;max-width:400px;width:300px'>";
            
            var inputValue = $obj.attr("inputValue");
            var _arr = inputValue.split(',');

            for (var im in json) {

                str += "<input name='"+$obj.attr("id")+"' type='checkbox' value='" + json[im].Key + "'";

                if (arrstrContrastItem(_arr, json[im].Key)) {

                    str += " checked='checked'";
                }
                str += "/>" + json[im].Value + "";
            }
            str += "</div><div class='reviseSaveBtn' onclick=reviseSubmit('"+$obj.attr("id")+"',getChecked('"+$obj.attr("id")+"').value,'checkbox')></div>";
            reviseFloat($obj,str);
        }
        function reviseForRaido($obj, json) {

            var str = "<div id='" + $obj.attr("id") + "revise' class='reviseCheckBox reviseForm' style='min-width:200px;max-width:400px;width:300px'>";

            var inputValue = $obj.attr("inputValue");
            var _arr = inputValue.split(',');

            for (var im in json) {

                str += "<input name='" + $obj.attr("id") + "' type='radio' value='" + json[im].Key + "'";

                if (arrstrContrastItem(_arr, json[im].Key)) {

                    str += " checked='checked'";
                }
                str += "/>" + json[im].Value + "";
            }
            str += "</div><div class='reviseSaveBtn' onclick=reviseSubmit('" + $obj.attr("id") + "',getChecked('" + $obj.attr("id") + "').value,'checkbox')></div>";
            reviseFloat($obj, str);
        }
        function getChecked(name){
            var checkArr = document.getElementsByName(name)
            var s = new Array()
            var v = new Array()
            for(var i=0;i<checkArr.length;i++) {
                if(checkArr[i].checked) {
                    s[s.length]= checkArr[i].nextSibling.nodeValue;
                    v[v.length]= checkArr[i].value;

                }    
            }
            return {text:s.join(','),value:v.join(',')};
        }

        function reviseSubmit(_name,_value,type) {

            $.get("/shop/Ajax_revise?id="+getPageShopId()+"&name="+_name+"&value="+escape(_value),function(str){
                if(str=="ok"){
                    reviseUpdate(type);
                }
            });
            
        
        }
        //更新目标值
        function reviseUpdate(type){
            var reviseFloatPanle = $("#reviseFloatPanle");
            var text,name,inputValue;
            _obj = reviseFloatPanle.get(0).children(0).children(0);
            name = _obj.name;
            obj = $("#"+name)
            switch(type){
                case "checkbox":
                    var _c = getChecked(name);
                    text = _c.text;
                    inputValue = _c.value;
                    break;
                case "select":
                    text = _obj.options[_obj.selectedIndex].text;
                    inputValue = _obj.options[_obj.selectedIndex].value;
                    break;
                case "tags":

                    inputValue = "<A>" + _obj.value.replace(/\,/g, "</A><A>") + "</A>";
                    text = inputValue;

                    break;
                default:
                    text = _obj.value;

                    inputValue = text;
                    break;
            }

            obj.html(text);
            obj.attr("inputValue",inputValue);
            reviseFloatPanle.empty();
            reviseFloatPanle.css("display","none")
            obj.attr("revise", "true");
        
        }

        function reviseFloat($obj,html){
            if($("#reviseFloatPanle").length==0){
                $(document.body).append("<div id='reviseFloatPanle'></div>");
            }
            var $reviseFloatPanle = $("#reviseFloatPanle");

            $reviseFloatPanle.html(html);

            elLocation($reviseFloatPanle, $obj, 0,-6,-5);
            $reviseFloatPanle.css("display","block")

        }
//***shop***出初化/
    function initComment() {
        $("#addCommentBtn").click(function () {
            var poatData = $("#comment").serializeArray();
            $.post("/Comment/add/"+getPageShopId(),poatData,function(text){
                
            });
        })

        $("#getCommentBtn").click(function () {
            $.get("/Comment/get/"+getPageShopId(),function(text){
                alert(text.toString())
            });
        })


    }
    //得到评论start:开始数,end:结束数
    function getComments(start, end) {
            if (start == undefined) { start = 0 }
            if (end == undefined) { end=2}
            $.get("/Comment/get/" + getPageShopId()+"?start="+start+"&end="+end, parseCommentJSON);       
        }
        function parseCommentJSON(){
 
            var json = arguments[0]

            for(var i=0;i<json.length;i++){
                
                writeCommentJSON(json[i])
            }
                
        }
        function writeCommentJSON(json){

            
            var newCommentNode = $("#commentTemp").clone(true);
            newCommentNode.attr("id",json["Id"]["val"]);
            var ns = newCommentNode.find("._var");

            ns.each(function () {
                var str = $(this).text();
                str = str.substring(1, str.length);
                var _val = eval("json" + str);


                if (str == '["userId"]') { _val = nnUserSet(_val) }

                if (_val != null && _val != "") {
                    $(this).get(0).innerHTML = _val;
                } else {
                    $(this).parent().hide();

                }

                if ($(this).get(0).className == "datetime _var") {$(this).get(0).innerHTML = JsonDateToDate($(this).get(0).innerHTML);}
            })
            $("#commentsBar").append(newCommentNode);
        }
        //菜添加-修改
        function addItem(){
            var formdata = $("#xxxx").serializeArray();

            $.get("/shop/Ajax_additem/"+getPageShopId(),formdata,function(json){alert(json)})            
        }

        //显示impressionForm
        function showImpressionForm(){
            elLocation($("#impressionForm"),$("#impressionBtn"),0,-12,30);
            $("#impressionForm").show();

            $("#impressionBtn").addClass("selected");

        }
        function hideImpressionForm(){
            $("#impressionBtn").removeClass("selected");
            $("#impressionForm").hide();

        }
        //印象记录添加
        function addImpression(){
            var _iwEl =  $("#impressionWord");
            var iw = _iwEl.val();
            if(iw==""){return}
            var formdata = $("#impressionForm").serializeArray();
            $.post("/impression/add/",formdata,function(json){
                writeImpressionJSON(json)
                _iwEl.val("");
                hideImpressionForm();
            })

        }
        function getImpressions(){

            $.get("/impression/get/"+getPageShopId(),parseImpressionJSON);
            
        }
        function getImpressionById(){
            
            //$.get("/ajax/impression/getById/"+getPageShopId(),parseImpressionJSON);
            
        }
        function parseImpressionJSON(){
            var json = arguments[0]

            for(var i=0;i<json.length;i++){
                
                writeImpressionJSON(json[i])
            }            
        }
        function writeImpressionJSON(json){

            var newImpression = $("#impressionTemp").clone(true);
            newImpression.attr("id",json["Id"]["val"]);
            var ns = newImpression.find("._var");

            ns.each(function () {
                var str = $(this).text();
                var _el = $(this).get(0);
                str = str.substring(1, str.length);
                var _val = eval("json" + str);
                if (str == '["userId"]') { _val = nnUserSet(_val) }

                
                _el.innerHTML = _val;

                if (_el.className == "datetime _var") {$(this).next().next().attr('title',JsonDateToDate(_el.innerHTML)) ;$(this).hide();};
            })

            $("#shopImpressionsBar").append(newImpression)            
        }

        //ajax印象记录删除:shopId,ImpressionWord 
        function delImpressionAjax(shopId,content,itemObj){

            $.get("/impression/delByShopIdByContent/"+shopId,{word:content},function(result){removeImpressionTag(result,itemObj)});
            
        
        }
        function removeImpressionTag(result,itemObj){
                if(result == "true"){
                    itemObj.remove()
                }
        }
        //解析shop.item的json集成
        function parseShopItemJson(jsonstr){
            if(!jsonstr){jsonstr = $("#shopItemJsonStr").html()};
            var json = eval(jsonstr);
            if(!jsonstr){return }
            var html=""
            for(var i=0;i<json.length;i++){
                html += "<div class='item'><a href='"+json[i].id+"' title='更新时间"+json[i].date+"'><span class='name'>"+json[i].name+"</span></a><span class='count'>"+json[i].count+"</span></div>"
            }

            $("#shopItemsBar").append(html);
        }
        function  getPageShopId(){
            return $("#shopPanle").attr("shopId")
        }
        //impression处理事件
        function impressionEvent(evt){
            evt = $.event.fix(evt)
            var el = $(evt.target);
            var item = el.parent().parent(".impression");
            var content = item.find(".content").text()
            var action = el.attr("class")
            
            //ajax del 该店的此印象字记录
            if(action == "delBtn"){

                var shopId = getPageShopId();
                delImpressionAjax(shopId,content,item)

            }


        }

        //允许编辑按键
        function abledEdit(el) {
            var str;
            if (el.getAttribute("edit") == "true") {
                str = "hidden";
                el.setAttribute("edit", "false");
                el.innerHTML ="Edit"
            } else {
                str = "visible";
                el.setAttribute("edit", "true");
                el.innerHTML = "disableEdit";
            }
            el.disabled = true;
            ObjectsReviseBtn.each(function() {

                $(this).css("visibility", str);
            })
            el.disabled = false;


        }
        //浏览更多图片
        var shopPics = new Array();
        function showMoreShopPic() {

            var $contentPanle = $('#contentPanle');
            var $picsPanle = $('#picsPanle')
            if (!$picsPanle.is(":hidden")) {
                $picsPanle.hide();
                $contentPanle.show();
                return;      
            }
            $picsPanle.show()
            $contentPanle.hide()

            if ($picsPanle.html().length>2) {return }
            $picsPanle.height($contentPanle.height());
            shopPics = $picsPanle.attr('content').replace(/></g,">||<").split("||");

            timerAddElement();
        }
        function timerAddElement() {

            var $picsPanle = $('#picsPanle');
            if (shopPics.length > 0) {
                //alert(shopPics.pop())

                $picsPanle.append(shopPics.pop())
            } else { return }
            setTimeout(timerAddElement, 100);
        }
        function getShopId() {
            return document.getElementById("shopPanle").getAttribute("shopId");
        }
        function changeShopMenu(it) {
            /*无刷新时的脚本-----遗弃
            var _lis = it.parentNode.children;
            for (var i = 0; i < _lis.length; i++) {
                var _dom = document.getElementById(_lis[i].getAttribute("target"))
                //alert(_dom.id)
                if (it == _lis[i]) {

                    if (_dom) {
                        _dom.style.display = "block";
                        var _cp = document.getElementById("contentPanle");
                        _cp.style.display = "block";
                        _dom.style.height = document.getElementById("contentPanle").scrollHeight + "px";
                        if (_dom != _cp) { _cp.style.display = "none" };
                        it.className = "openMorePicBtnCls";
                    }

                } else {
                    if (_dom) {

                        _dom.style.display = "none";

                        _lis[i].className = "closeMorePicBtnCls";
                   }
                }
            }
            无刷新时的脚本-----遗弃*/

            //for 刷新页面channel显示 适用SEO
            it.className = "openMorePicBtnCls";
        }



        /*tag*/
        function selectTagsAPP(id, tagId, fromId) {
            var untagBox = document.getElementById(id);
            var tagBox = document.getElementById(tagId);
            tagBox.setAttribute("target", id)
            str = untagBox.innerHTML
            untagBox.setAttribute("target", tagId)
            var tags = str.split(",")
            var html = new Array();
            for (var i = 0; i < tags.length; i++) {
                html[i] = "<a class='shopTag' href='###' onclick='selectTagsAPP_act(this)'>";
                html[i] += tags[i];
                html[i] += "</a>";
            }
            untagBox.innerHTML = html.join("");
        }
        function selectTagsAPP_act(it,id) {

            //document.getElementById(it.parentNode.getAttribute("target")).appendChild(it)
            //it.parentNode.getAttribute("target")

            addTags(it.innerText, id);


        }
        function addTags(str, id) {

            if (!vTag(str)) { alert("err"); return }
            var tagBox = document.getElementById(id)

            var tags = tagBox.value.split(",");
            if (tags[0] == "") { tags.shift() }
            for (var i = 0; i < tags.length; i++) {
                if (tags[i] == str) { return }
            }

            tags.push(str)

            tagBox.value = tags.join(",")

        }
        function addNewTag(id, targetID) {

            var newTagInput = document.getElementById(id);

            addTags(newTagInput.value, targetID)
        }
        //tag有效验证
        function vTag(str) {
            if (str == "") { return false }
            if (str.indexOf(",") >= 0) { return false }
            return true;
        }